package com.xls.dormitoryservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.exception.BusinessException;
import com.xls.dormitoryservice.entity.Room;
import com.xls.base.model.Student;
import com.xls.dormitoryservice.entity.Repair;
import com.xls.dormitoryservice.mapper.RepairMapper;
import com.xls.dormitoryservice.mapper.RoomMapper;
import com.xls.base.mapper.StudentMapper;
import com.xls.dormitoryservice.service.RepairService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xls.dormitoryservice.vo.RepairVo;
import com.xls.dormitoryservice.vo.RepairQuery;
import com.xls.entity.SecurityUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {
    
    @Autowired
    private RepairMapper repairMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private StudentMapper studentMapper;
    
    @Override
    public RepairVo getRepair(String id) {
        return repairMapper.getRepair(id);
    }

    @Override
    public RepairVo saveRepair(RepairVo repairVo) {
        if (repairVo.getRoomId() != null && "-1".equals(repairVo.getRoomId().toString())) {
            SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = securityUser.getUsername();
            Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("sno", username));
            Room room = roomMapper.selectById(student.getRoomId());
            repairVo.setRoomId(room.getId());
            repairVo.setApplyTime(new Date());
        }
        Repair repair = new Repair();
        BeanUtils.copyProperties(repairVo, repair);
        validateRepair(repairVo, repair);
        this.save(repair);
        return repairMapper.getRepair(repair.getId().toString());
    }

    @Override
    public RepairVo updateRepair(RepairVo repairVo) {
        Repair repair = new Repair();
        BeanUtils.copyProperties(repairVo, repair);
        validateRepair(repairVo, repair);
        this.updateById(repair);
        return repairMapper.getRepair(repair.getId().toString());
    }

    private void validateRepair(RepairVo repairVo, Repair repair) {
        repair.setStatus(Integer.valueOf(repairVo.getStatus()));
        if (repair.getRoomId() == null) {
            Room room = roomMapper.selectOne(new QueryWrapper<Room>()
                    .eq("building", repairVo.getBuilding())
                    .eq("floor", repairVo.getFloor())
                    .eq("rno", repairVo.getRno()));
            if (room == null) {
                throw new BusinessException(77777, "没有此宿舍");
            } else {
                repair.setRoomId(room.getId());
            }
        }
    }

    @Override
    public Page<RepairVo> getRepairVoPage(Integer page, Integer limit, RepairQuery repairQuery) {
        QueryWrapper<Repair> repairQueryWrapper = new QueryWrapper<>();
        String building = repairQuery.getBuilding();
        String floor = repairQuery.getFloor();
        String rno = repairQuery.getRno();
        if ("-1".equals(rno)) {
            SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = securityUser.getUsername();
            Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("sno", username));
            if (student == null) {
                throw new BusinessException(8888, "管理员不允许操作");
            }
            if (student.getRoomId() == null) {
                throw new BusinessException(8888, "请先入住");
            }
            Room room = roomMapper.selectById(student.getRoomId());
            building = room.getBuilding();
            floor = room.getFloor();
            rno = room.getRno().toString();
        }
        String goodsName = repairQuery.getGoodsName();
        String applyBegin = repairQuery.getApplyBegin();
        String applyEnd = repairQuery.getApplyEnd();
        String status = repairQuery.getStatus();
        String phone = repairQuery.getPhone();
        String repairBegin = repairQuery.getRepairBegin();
        String repairEnd = repairQuery.getRepairEnd();
        repairQueryWrapper
                .eq(!StringUtils.isEmpty(building), "r.building", building)
                .eq(!StringUtils.isEmpty(floor), "r.floor", floor)
                .likeRight(!StringUtils.isEmpty(rno), "r.rno", rno)
                .likeRight(!StringUtils.isEmpty(goodsName), "rp.goods_name", goodsName)
                .gt(!StringUtils.isEmpty(applyBegin), "rp.apply_time", applyBegin)
                .lt(!StringUtils.isEmpty(applyEnd), "rp.apply_time", applyEnd)
                .eq(!StringUtils.isEmpty(status), "rp.status", status)
                .likeRight(!StringUtils.isEmpty(phone), "rp.phone", phone)
                .gt(!StringUtils.isEmpty(repairBegin), "rp.repair_time", repairBegin)
                .lt(!StringUtils.isEmpty(repairEnd), "rp.repair_time", repairEnd)
                .eq("rp.is_deleted", 0);
        return repairMapper.getRepairVoPage(new Page<>(page, limit), repairQueryWrapper);
    }
}
